﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

using MvcAtletIS.Models;
using PagedList;

namespace MvcAtletIS.Controllers
{
    public class HomeController : Controller
    {
        private atletisEntities db = new atletisEntities();

        private const int WORLD_CATEGORY = 2;
        private const int VIDEO_CATEGORY = 3;

        public ActionResult Index(int? page)
        {
            string sInfoPath = Server.MapPath("~/Content/info.html");
            if (System.IO.File.Exists(sInfoPath))
            {
                StreamReader sr = new StreamReader(sInfoPath, Encoding.Default);
                ViewBag.InfoBox = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
            }
            else
            {
                ViewBag.InfoBox = string.Empty;
            }
            
            ViewBag.LinksCategories = db.LinksCategories.Where(lc => lc.Active == true).OrderBy(lc => lc.Order).ToList();
            ViewBag.Links = db.Links.Where(l => l.Active == true);

            ViewBag.VideoGallery = db.Articles
                .Where(a => a.CategoryId == VIDEO_CATEGORY & a.Active == true)
                .OrderByDescending(a => a.Date)
                .Take(3)
                .ToList();
            
            int MainCategory = 1;
            ViewBag.TopArticles = db.Articles.Where(a => a.CategoryId == MainCategory & a.Active == true & a.Top == true).OrderByDescending(a => a.Date).Take(3).ToList();
            var model = db.Articles.Where(a => a.CategoryId == MainCategory & a.Active == true & a.Top == false).OrderByDescending(a => a.Date);

            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(model.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult WorldNews(int? page)
        {
            var model = db.Articles
                .Where(a => a.CategoryId == WORLD_CATEGORY & a.Active == true)
                .OrderByDescending(a => a.Date);

            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(model.ToPagedList(pageNumber, pageSize));
        }

        public ViewResult VideoGallery(int? page)
        {
            var model = db.Articles
                .Where(a => a.CategoryId == VIDEO_CATEGORY & a.Active == true)
                .OrderByDescending(a => a.Date);

            int pageSize = 4;
            int pageNumber = (page ?? 1);
            return View(model.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult PrivacyPolicyInMobileApps()
        {
            return View();
        }

        public ViewResult PhotoGallery()
        {
            return View();
        }

        public ActionResult Administration()
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Users"))
                {
                    return RedirectToAction("Error", new { title = "Zamietnutý prístup", message = "Nemáte právo na prístup do Administrácie." });
                }

                if (User.IsInRole("Administrators"))
                {
                    return View();
                }
            }

            return RedirectToAction("LogOn", "Account");
        }

        public ActionResult Error(string title, string message)
        {
            ViewBag.MyTitle = title;
            ViewBag.MyMessage = message;
            return View();
        }
    }
}
